package com.easymin.weixin.manager.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.common.api.ErrandApi;
import com.easymin.weixin.common.api.FreightApi;
import com.easymin.weixin.common.api.PassengerApi;
import com.easymin.weixin.common.api.SalesApi;
import com.easymin.weixin.common.api.ZhuanApi;
import com.easymin.weixin.controller.v4.ResultTransport;
import com.easymin.weixin.entity.freight.Order;
import com.easymin.weixin.entity.passenger.Passenger;
import com.easymin.weixin.entity.sales.Coupon;
import com.easymin.weixin.manager.FreightManager;

@Service
public class DefaultFreightManager implements FreightManager { 
  @Autowired
  private FreightApi freightApi;

  @Autowired
  private PassengerApi passengerApi;

  @Autowired
  private SalesApi salesApi;

  @Autowired
  private DaijiaApi daijiaApi;

  @Autowired
  private ZhuanApi zhuanApi;

  @Autowired
  private ErrandApi errandApi;

  @Override
  public Map<String, Object> create(Long passengerId, String passengerName, String passengerPhone,
      Long companyId, String companyName, Long truckTypeId, String truckTypeName,
      String channelName, Long startTime, String waypoint, String memo, Boolean flitting,
      Boolean receipt, Boolean receivedPay, Double payMount, Double preMileagePrice, Long couponId,
      Double tip, Double preStartPrice, Double preMileage, Integer preRunTime,
      Double preRunTimePrice, Double budgetPay, String payDetail, String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    Passenger passenger = passengerApi.findById(passengerId, appKey);
    if (null == passenger) {
      map.put("code", -4);
      map.put("msg", "乘客不存在!");
      return map;
    }

    if (passenger.getInBlackList()) {
      map.put("code", -6);
      map.put("msg", "下单失败!");
      return map;
    }
    
    Coupon coupon = null;
    if (null != couponId) {
      // 查询优惠券
      Map<String, Object> coupons = salesApi.coupons(couponId, appKey);
      int code = (int) coupons.get("code");
      if (code == 0) {
        coupon = (Coupon) coupons.get("data");
        Integer status = coupon.getStatus();
        if (status != 1) {
          map.put("code", -10);
          map.put("msg", "优惠券无效");
          return map;
        }
      } else {
        map.put("code", -11);
        map.put("msg", (String) coupons.get("message"));
        return map;
      }
    }
    passengerPhone = passengerPhone.replaceAll(" ", "");
    passengerPhone = passengerPhone.replaceAll("-", "");
    map =
        freightApi.create(passengerId, passengerName, passengerPhone, companyId, companyName,
            truckTypeId, truckTypeName, channelName, startTime, waypoint, memo, flitting, receipt,
            receivedPay, payMount, preMileagePrice, couponId, tip, preStartPrice, preMileage,
            preRunTime, preRunTimePrice, budgetPay, payDetail, appKey);
    int code = (int) map.get("code");
    if (code == 0) {
      if (couponId != null) {
        salesApi.update(couponId, 2, appKey);
      }
    }

    return map;
  }

  @Override
  public Map<String, Object> saveRoadLine(String waypoints, Long passengerId, String addressName,
      String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();

    map = freightApi.saveRoadLine(waypoints, passengerId, addressName, appKey);

    return map;
  }

  @Override
  public Map<String, Object> deleteRoadLine(Long id, String appKey) {
    return freightApi.deleteRoadLine(id, appKey);
  }

  @Override
  public Map<String, Object> findRoadLine(Long passengerId, String appKey) {
    return freightApi.findRoadLine(passengerId, appKey);
  }


  @Override
  public Map<String, Object> findByOrderId(Long orderId, String appKey) {
    return freightApi.findByOrderId(orderId, appKey);
  }

  @Override
  public Map<String, Object> findOrders(
      Long passengerId, Integer page, Integer rows, String appKey) {
    return freightApi.findOrders(passengerId, page, rows, appKey);
  }

  @Override
  public Map<String, Object> review(Long orderId, String content, Double score, String appKey) {
    return freightApi.review(orderId, content, score, appKey);
  }

  @Override
  public String hasten(Long orderId, String appKey) {
    return freightApi.hasten(orderId, appKey);
  }

  @Override
  public Map<String, Object> cancel(Long orderId, String cause, Boolean isDriver, String appKey) {
    return freightApi.cancel(orderId, cause, isDriver, appKey);
  }

  @Override
  public Order findOneOrder(Long orderId, String appKey) {
    return freightApi.findOneOrder(orderId, appKey);
  }

  @Override
  public Map<String, Object> reach(Long orderId, Long waypointId, String appKey) {
    return freightApi.reach(orderId, waypointId, appKey);
  }

  @Override
  public Map<String, Object> jiedan(Long orderId, String appKey) {
    return freightApi.jiedan(orderId, appKey);
  }

  @Override
  public Map<String, Object> grabOrder(Long orderId, Long employId, String employName,
      String employPhone, String employNo, Long employCompanyId, String employPhoto,
      Double employScore, String appKey) {
    return freightApi.grabOrder(orderId, employId, employName, employPhone, employNo,
        employCompanyId, employPhoto, employScore, appKey);
  }

  @Override
  public Map<String, Object> runOrder(Long orderId, String appKey) {
    return freightApi.runOrder(orderId, appKey);
  }

  @Override
  public Map<String, Object> refuse(Long orderId, Long employId, String cause, String appKey) {

    return freightApi.refuse(orderId, employId, cause, appKey);
  }

  @Override
  public Map<String, Object> wait(Long orderId, String appKey) {
    return freightApi.waitOrder(orderId, appKey);
  }

  @Override
  public ResultTransport findOrderV4(Long orderId, String address, String appKey) {
    return freightApi.findOrderV4(orderId, address, appKey);
  }

  @Override
  public ResultTransport queryTypeV4(Long companyId, String appKey) {
    return freightApi.queryTypeV4(companyId, appKey);
  }

  @Override
  public ResultTransport grabV4(Long orderId, Long employId, String employName, String employPhone,
      Long companyId, String employAccount, String employPhoto, Double employScore, String appKey) {
    return freightApi.grabV4(orderId, employId, employName, employPhone, companyId, employAccount,
        employPhoto, employScore, appKey);
  }

  @Override
  public ResultTransport jiedanV4(Long orderId, String appKey) {
    return freightApi.jiedanV4(orderId, appKey);
  }

  @Override
  public ResultTransport refuseV4(Long orderId, String cause, String appKey) {
    return freightApi.refuseV4(orderId, cause, appKey);
  }

  @Override
  public ResultTransport runV4(Long orderId, String appKey) {
    return freightApi.runV4(orderId, appKey);
  }

  @Override
  public ResultTransport waitV4(Long orderId, String appKey) {
    return freightApi.waitV4(orderId, appKey);
  }

  @Override
  public ResultTransport cancelV4(Long orderId, String cause, String appKey) {
    return freightApi.cancelV4(orderId, cause, appKey);
  }

  @Override
  public ResultTransport reachV4(Long orderId, Long waypointId, String appKey) {
    return freightApi.reachV4(orderId, waypointId, appKey);
  }

  @Override
  public Long countNotPayAmounts(Long passengerId, String appKey) {
    return freightApi.countNotPayAmounts(passengerId, appKey);
  }

  @Override
  public Long countRunning(Long passengerId, String appKey) {
    return freightApi.countRunning(passengerId, appKey);
  }

  @Override
  public Long countNotFinishedByPassenger(Long passengerId, String appKey) {
    return freightApi.countNotFinishedByPassenger(passengerId, appKey);
  }

}
